package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import com.Global.Global;
import com.code.chart.AbstractDemoChart;

public class Chart_Call_MonthInYear extends AbstractDemoChart {

	private int iMaxY = 0;
	private int iMonthNowPlus = Calendar.getInstance().get(Calendar.MONTH) + 2;
	private int iMonthNow = Calendar.getInstance().get(Calendar.MONTH) + 1;
	private int iYearNowAbt = Calendar.getInstance().get(Calendar.YEAR) - 1;
	private int iYearNow = Calendar.getInstance().get(Calendar.YEAR);

	public String getName() {
		return null;
	}

	public String getDesc() {
		return null;
	}

	private double[] getCallInYear(String strSubQuery) {
		double dbThangSLGoi[] = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0 };
		String sql = "SELECT MONTH(Ngay) Thang, COUNT(*) SL" + " FROM CuocGoi"
				+ " WHERE Ngay > = '" + String.valueOf(iYearNowAbt) + "/"
				+ String.valueOf(iMonthNowPlus) + "/1" + "' " + strSubQuery
				+ " GROUP BY MONTH(Ngay) ORDER BY MONTH(Ngay)";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				int iMonthIndex = Integer.parseInt(rs.getString("Thang")
						.toString());
				int iSLIndex = Integer.parseInt(rs.getString("SL").toString());
				dbThangSLGoi[returniMonth(iMonthIndex - iMonthNow + 12) - 1] = (double) iSLIndex;

				// set max Y
				if (iSLIndex > iMaxY)
					iMaxY = iSLIndex;
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dbThangSLGoi;
	}

	public Intent execute(Context context) {
		double dbAllCallInYear[] = getCallInYear("");
		double dbCallHasSPInYear[] = getCallInYear(" AND SoPhieu IS NOT NULL");
		String[] titles = new String[] { "Tất cả", "Đã bán" };
		List<double[]> values = new ArrayList<double[]>();
		values.add(dbAllCallInYear);
		values.add(dbCallHasSPInYear);
		int[] colors = new int[] { Color.BLUE, Color.GREEN };
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		iMaxY = iMaxY * 12 / 10;
		setChartSettings(renderer, "", "Tháng", "", 0.5, 12.5, 0, iMaxY,
				Color.GRAY, Color.LTGRAY);
		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		renderer.setXLabels(0);
		renderer.setYLabels(10);
		for (int i = iMonthNowPlus; i < iMonthNowPlus + 12; i++) {
			renderer.addXTextLabel(i - iMonthNowPlus + 1, returnstrMonth(i));
		}
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.CENTER);
		renderer.setPanEnabled(true, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);
		renderer.setZoomButtonsVisible(true);  
		return ChartFactory.getBarChartIntent(
				context,
				buildBarDataset(titles, values),
				renderer,
				Type.STACKED,
				"TK cuộc gọi từ " + String.valueOf(iMonthNowPlus) + "/"
						+ String.valueOf(iYearNowAbt) + " đến "
						+ String.valueOf(iMonthNow) + "/"
						+ String.valueOf(iYearNow) + " (Lần gọi/tháng)");
	}

	public String returnstrMonth(int iMonth) {
		if (iMonth == 12 || iMonth == 0)
			return String.valueOf(12);
		else
			return String.valueOf(iMonth % 12);
	}

	public int returniMonth(int iMonth) {
		if (iMonth == 12 || iMonth == 0)
			return 12;
		else
			return iMonth % 12;
	}
}
